




# class DingTalkLogin(APIView):
#     def get(self, request):
#         from urllib.parse import quote
#         params = [
#             f"redirect_uri={quote('http://127.0.0.1:8080/createuser/')}",
#             "response_type=code",
#             "client_id=dingqxjco4n5jjtt7ctj",
#             "scope=openid",
#             "prompt=consent"
#         ]
#         url = "https://login.dingtalk.com/oauth2/auth?" + ("&".join(params))
#         return Response({"url": url})
#
#
# import request
#
#
# class DingTalkCallback(APIView):
#     def get(self, request):
#         authCode = request.query_params.get('code')
#
#         # 根据authCode获取用户accessToken
#         data = {
#             "clientId": "dingqxjco4n5jjtt7ctj",
#             "clientSecret": "cQJnGlcoMmz6Nnv-r0aFEpQAHqiwVK0t4yf1J_9Do8jQP1AV81iVT2M3GXHWLy53",
#             "code": authCode,
#             "grantType": "authorization_code"
#         }
#         resp = requests.post('https://api.dingtalk.com/v1.0/oauth2/userAccessToken', json=data).json()
#         accessToken = resp.get('accessToken')
#         uid = resp.get('openId')
#
#         # 根据accessToken获取用户信息
#         headers = {"x-acs-dingtalk-access-token": accessToken}
#         resp = requests.get('https://api.dingtalk.com/v1.0/contact/users/me', headers=headers).json()
#
#         name = resp.get('nick')
#
#         phone = resp.get('mobile')
#
#         # 登录，写入三方登录表和用户表
#         sfl = Sflogin.objects.filter(uid=uid, types=1).first()
#         if not sfl:
#             # 写入用户表
#             # 手机号用户存在直接绑定,不存在注册新用户
#             user = Susers.objects.filter(phone=phone).first()
#             if not user:
#                 userinfo = {
#                     "name": name,
#                     "phone": phone,
#                     "password": phone,
#                     "username": phone
#                 }
#                 us = SfUserSerializer(data=userinfo)
#                 if us.is_valid():
#                     user = us.save()
#                 else:
#                     return Response({"code": 10001, "msg": tostr(us.errors)})
#             # 写入三方登录表
#             sfinfo = {
#                 "types": 1,
#                 "token": accessToken,
#                 "uid": uid,
#                 "userid": user.id
#             }
#             sfs = SfLoginSerializer(data=sfinfo)
#             if sfs.is_valid():
#                 sfs.save()
#             else:
#                 return Response({"code": 10001, "msg": tostr(sfs.errors)})
#         else:
#             user = sfl.userid
#             sfl.token = accessToken
#             sfl.save()
#
#         # 生成jwt token并返回前端
#         payload = {"userid": user.id, "username": user.username, "exp": int(time.time()) + 60 * 60 * 2}
#         token = myjwt.jwt_encode(payload)
#         payload["exp"] = int(time.time() + 60 * 60 * 24 * 2)
#         refreshToken = myjwt.jwt_encode(payload)
#         query = [f"userid={payload['userid']}", f"username={payload['username']}", f"token={token}",
#                  f"refreshToken={refreshToken}"]
#         return Response({"code": 200, 'token': token, "userid": user.id, "retoken"})
